package cn.tedu.meituanprogram.controller;


import cn.tedu.meituanprogram.mapper.ProductMapper;
import cn.tedu.meituanprogram.pojo.Dto.ProductDto;
import cn.tedu.meituanprogram.pojo.Dto.ProductUpdateDto;
import cn.tedu.meituanprogram.pojo.entity.Product;
import cn.tedu.meituanprogram.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {
    // @Value() 的作用是给变量赋值,${xxx}会去application.properties里面读取配置信息
    @Value("$dirPath")
    private String dirPath;

    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("insert")
    public void insert(@RequestBody ProductDto product){
       System.out.println("111111111111:"+product);

       Product p = new Product();
        BeanUtils.copyProperties(product,p);
        p.setCreated(new Date());
        //保存到数据库
        mapper.insert(p);

    }

    @RequestMapping("select")
    public List<ProductDetailVo> select(){


        return mapper.select();
    }

    @RequestMapping("selectByCid")
    public List<ProductDetailVo> selectByCid(int id){
        return mapper.selectByCid(id);
    }

    @RequestMapping("selectByPid")
    public List<ProductDetailVo> selectByPid(int id,int cId){
        System.out.println("======================:"+id);
        System.out.println(mapper.selectByCid(id));
        return mapper.selectByPid(id,cId);
    }




/*
============================================================================================
*/

    @RequestMapping("delete")
    public void delete(int id){
        //得到图片路径 然后删除图片
        String url = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        //删除数据库数据
        mapper.deleteById(id);
    }

    @RequestMapping("selectById")
    public ProductUpdateVo selectById(int id){

            return mapper.selectById(id);
    }

    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDto product){
        mapper.update(product);
    }

    @RequestMapping("selectForIndex")
    public List<ProductIndexVo> selectForIndex(){
        return mapper.selectForIndex();
    }

    @RequestMapping("selectForTop")
    public List<ProductTopVo> selectForTop(){
        return mapper.selectForTop();
    }


    @RequestMapping("selectByWd")
    public List<ProductIndexVo> selectByWd(String wd){
        return mapper.selectByWd(wd);
    }

    @RequestMapping("selectForDetailById")
    public ProductDetailVo selectForDetailById(int id, HttpSession session){
        /*==============================设置增加浏览次数,且避免刷新页面增加浏览次数===================================*/
        //通过view从会话中获取字符串,如果取到的是null代表第一次浏览,不是null代表不是第一次浏览
        String info = (String) session.getAttribute("view"+id);
        if(info==null){
            //如果是第一次,需要把字符串保存到会话对象中,并且浏览加1
            session.setAttribute("view"+id,"visited");
            //客户端发送来请求,响应一次 去数据库浏览量就加1
            mapper.selectViewCountById(id);
        }

        return mapper.selectForDetailById(id);
    }

}
